/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.FermentationNotes;
import com.openbrew.openbrewrf.Fv;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import service.enumerations.MovementState;
import utils.SRMToRGB;

/**
 *
 * @author chapmand
 */
public class TankTools {
    private final NumberFormat f3 = new DecimalFormat("#0.000");
    private final NumberFormat f1 = new DecimalFormat("#0.0");
    
    public BufferedImage createIcon(final String imgName, Brews brew, Fv vessel){
        try{
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            BufferedImage img = ImageIO.read(classloader.getResource(imgName));
            Graphics g = img.getGraphics();
            Graphics2D g2d = (Graphics2D) g;

            RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            rh.put(RenderingHints.KEY_RENDERING,
                   RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHints(rh);

            //Set Bar to show how much is filled.
            //Set Location and size of bar, only modify this section.
            final int IMAGE_WIDTH = 250;
            final int IMAGE_HEIGHT = 400;
            final int BAR_X = 40;           //Sets the distance in the X-Dimension.
            final int BAR_WIDTH = 30;       //Sets the width of the bar in pixels
            final int BAR_HEIGHT = 195;     //Sets the hight of the bar in pixels
            final int BAR_Y_START = 45;     //Sets the distance in the Y-Dimension.
            final int TEXT_Y = 60;          //Sets the text Y-Dimension
            final int TEXT_X = 80;          //Sets the text X-Dimension
            final int LINE_SPACE = 14;      //Sets the line spacing for text.
            final Font font = new Font("Arial", Font.BOLD, 14);
            final Color colorWhite = new Color(255,255,255,126);

            if(brew!=null){
                //Find percentage of tank that is filled.  Amount in tank / capacity.
                double pctFill = (double)(brew.getVolLeft() / vessel.getFVCap());
                if(pctFill > 1){
                    pctFill = 1;
                }
                //Now find amount filled.
                int fillHeight = (int) Math.round((double)BAR_HEIGHT * pctFill);
                int emptyHeight = BAR_HEIGHT - fillHeight;
                //Get Color of beer
                String rgb = SRMToRGB.getRGB((double)brew.getBBeerID().getbSrm());
                final Color beerColor = SRMToRGB.getColorFromRGB(rgb);
                g2d.setColor(colorWhite);
                g2d.fillRect(BAR_X, BAR_Y_START, BAR_WIDTH, emptyHeight);
                g2d.setColor(beerColor);
                g2d.fillRect(BAR_X, emptyHeight + BAR_Y_START, BAR_WIDTH, fillHeight);
            } else {
                g2d.setColor(colorWhite);
                g2d.fillRect(BAR_X, BAR_Y_START, BAR_WIDTH, BAR_HEIGHT);
            }

            //Get the last known fermentation entry;
            String temp = "N/A";
            String grav = "N/A";
            String volume = "0";
            String id = "N/A";
            String name = "None";
            String status = "EMPTY";
            if(brew != null){
                if(brew.getFermentationNoteCollection()!=null && !brew.getFermentationNoteCollection().isEmpty()){
                    FermentationNotes fn = brew.getFermentationNoteCollection().get(brew.getFermentationNoteCollection().size()-1);
                    temp = Float.toString(fn.getFermNotesTemp());
                    grav = f3.format(fn.getFermNotesGravity());
                }
                volume = f1.format(brew.getVolLeft());
                id = Integer.toString(brew.getBid());
                name = brew.getBBeerID().getbName();
                status = MovementState.getState(brew.getBState()).toString();
            }

            //Fill Text Block Rectangle
            Color transColor = new Color(156,156,156,180);
            g2d.setColor(transColor);
            g2d.fillRoundRect(TEXT_X - 5, TEXT_Y-25, IMAGE_WIDTH - TEXT_X, 250, 25, 25);

            //Now Display Text Data On Text Block Area.
            g2d.setColor(Color.BLACK);
            g2d.setFont(font);
            int textY = TEXT_Y;
            g2d.drawString(vessel.getFVDesg(), TEXT_X, textY);
            textY += LINE_SPACE;
            g2d.drawString("Batch: " + id, TEXT_X, textY);
            textY += LINE_SPACE;
            g2d.drawString("Beer: " + name, TEXT_X, textY);
            textY += LINE_SPACE;
            g2d.drawString("Temp: " + temp, TEXT_X, textY);
            textY += LINE_SPACE;
            g2d.drawString("Grav: " + grav, TEXT_X, textY);
            textY += LINE_SPACE;
            g2d.drawString("Volume: " + volume + " BBL", TEXT_X, textY);
            textY += LINE_SPACE;
            textY += LINE_SPACE;
            g2d.drawString(status, TEXT_X, textY);

            //Dispose graphics
            g2d.dispose();
            g.dispose();
            
            return img;
        }catch(java.io.IOException e){
            
        }
        return null;
    }
}
